/**
 */

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

/**
 * @author Araya Zaesim, Michelle Partogi, Adam Perez, Michelle Partogi
 * @version 1.0
 */
public class ShortRangeChart extends JPanel {

	/**
	 * Field cList.
	 */
	private final CircleList cList; // added this

	/**
	 * Field gui.
	 */
	protected SpaceTraderGui gui = null;

	/**
	 * Field player.
	 */
	protected Player player = null;

	/**
	 * Field currSystem.
	 */
	private SolarSystem currSystem;

	/**
	 * Field tarSystem.
	 */
	private SolarSystem tarSystem;

	/**
	 * Field maxCurrDistance.
	 */
	private double maxCurrDistance;

	/**
	 * Field fuelEfficiency.
	 */
	private double fuelEfficiency;
	
	/**
	 * Dimensions for chart.
	 */
	private static final int DIM = 300;
	
	/**
	 * Oval size
	 */
	private static final int OVAL_SIZE = 7;
	
	/**
	 * Pilot multiplier
	 */
	private static final double PILOT_MULT = 1.5;

	/**
	 * A getter for the user's current system.
	 * 
	
	 * @return The current system. */
	public SolarSystem getCurrSystem() {
		return currSystem;
	}

	/**
	 * A setter for the current system (used after a player warps)
	 * 
	 * @param currSystem
	 *            The updated current system.
	 */
	public void setCurrSystem(SolarSystem currSystem) {
		this.currSystem = currSystem;
		tarSystem = null;
		repaint();
	}

	/**
	 * Constructor for the Short Range Chart.
	 * 
	 * @param system
	 *            The array of systems.
	 * @param gui
	 *            The GUI
	 * @param player
	 *            The player object which holds his/her data
	 */
	public ShortRangeChart(List<SolarSystem> system, SpaceTraderGui gui,
			Player player) {
		setBackground(Color.black);
		setPreferredSize(new Dimension(DIM, DIM));
		setLayout(null);
		// repaint();
		this.player = player;
		this.cList = new CircleList(); // added this
		this.gui = gui;
		tarSystem = null;
		for (SolarSystem planet : system) {
			cList.add(new Circle(planet));
		}
		addMouseListener(new MouseClick());
		updateRange();
	}

	/*
	 * public void updateColor(SolarSystem currSystem) { Graphics g =
	 * getGraphics(); super.paintComponent(g); repaint();
	 * 
	 * for(Circle c : cList.getCircle()) {
	 * if(c.system.getName().equals(currSystem.getName()) {
	 * g.setColor(Color.GREEN); g.fillOval(c.x, c.y, 5, 5); g.drawString(c.name,
	 * c.x, c.y); } } }
	 */
	/**
	 * Draws the chart.
	 * @param g Graphics
	 */

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		for (Circle c : cList.getCircle()) {
			g.setColor(c.color);
			if (currSystem != null) {
				if (c.system.equals(currSystem)) {
					g.setColor(Color.GREEN);
					g.drawOval(currSystem.getX() - (int) maxCurrDistance,
							currSystem.getY() - (int) maxCurrDistance,
							(int) maxCurrDistance << 1,
							(int) maxCurrDistance << 1);
				} else if (c.system.equals(tarSystem)) {
					if (equalsInRange(currSystem, tarSystem)) {
						g.setColor(Color.BLUE);
					} else {
						g.setColor(Color.RED);
					}
				}
			}
			g.fillOval(c.x, c.y, OVAL_SIZE, OVAL_SIZE);
			g.drawString(c.name, c.x, c.y);
		}
	}

	/**
	 * Returns whether or not the target solar system is in range of the ship
	 * 

	 * @param current SolarSystem
	 * @param target SolarSystem
	
	 * @return boolean */
	public boolean equalsInRange(SolarSystem current, SolarSystem target) {
		fuelEfficiency = (player.getShip().getMaxDistance() + (player
				.getPilotPoints() / PILOT_MULT)) / player.getShip().getMaxFuel();
		maxCurrDistance = fuelEfficiency * player.getShip().getFuel();
		final double distance = target.getDistance(current);
		return (distance < maxCurrDistance);
	}

	/**
	 * Method updateRange.
	 */
	public final void updateRange() {
		fuelEfficiency = (player.getShip().getMaxDistance() + (player
				.getPilotPoints() / PILOT_MULT)) / player.getShip().getMaxFuel();
		maxCurrDistance = fuelEfficiency * player.getShip().getFuel();
	}

	/**
	 * Method setTarSystem.
	 * @param system SolarSystem
	 */
	public void setTarSystem(SolarSystem system) {
		this.tarSystem = system;
		System.out.println("Set " + tarSystem.getName());
		repaint();
	}

	/**
	 * @author Araya Zaesim, Stanley Ayzenberg, Michelle Partogi, Adam Perez
	 */
	private class MouseClick extends MouseAdapter { // the interface needs
														// all of this
		/**
		 * toString method.
		
		 * @return String */
		public String toString() {
			return "";
		}

		/**
		 * Method mouseClicked.
		 * @param event MouseEvent
		
		 * @see java.awt.event.MouseListener#mouseClicked(MouseEvent) */
		public void mouseClicked(MouseEvent event) {
			final Point coord = event.getPoint();
			final SolarSystem system = cList.click((int) coord.getX(),
					(int) coord.getY());
			if (system != null) {
				gui.updateTargetSystem(system);
				// gui.updateSolarSystem();
				// gui.updateMarketPlace(player);
				// call function in GUI that'll update target universe info
			}
		}// end mouseClicked
	}// end MouseClick

	/**
	 * @author Araya Zaesim, Stanley Ayzenberg, Michelle Partogi, Adam Perez
	 */
	private static class Circle {
		/**
		 * Field system.
		 */
		public SolarSystem system;

		/**
		 * Field rad.
		 */
		/**
		 * Field y.
		 */
		/**
		 * Field x.
		 */
		public int x, y, rad;

		/**
		 * Field name.
		 */
		public String name;

		/**
		 * Field color.
		 */
		public Color color;

		/**
		 * toString method.
		
		 * @return String */
		public String toString() {
			return "";
		}
		
		/**
		 * Constructor for Circle.
		 * @param system SolarSystem
		 */
		private Circle(SolarSystem system) { // int x, int y, String name){
			this.system = system;
			this.color = Color.white;
			this.x = system.getX();
			this.y = system.getY();
			this.name = system.getName();
			this.rad = OVAL_SIZE;// player.getPilotPoints()+player.getShip().getFuel();
		}
	}

	/**
	 * @author Araya Zaesim, Stanley Ayzenberg, Michelle Partogi, Adam Perez
	 */
	private static class CircleList {
		/**
		 * toString method.
		
		 * @return String */
		public String toString() {
			return "";
		}
		
		/**
		 * Field circles.
		 */
		private final List<Circle> circles;

		/**
		 * Constructor for CircleList.
		 */
		private CircleList() {
			circles = new ArrayList<Circle>();
		}

		/**
		 * Method add.
		 * @param circ Circle
		 */
		public void add(Circle circ) {
			circles.add(circ);
		}

		/**
		 * Method getCircle.
		
		 * @return ArrayList<Circle> */
		public List<Circle> getCircle() {
			return circles;
		}

		/**
		 * Method click.
		 * @param x int
		 * @param y int
		
		 * @return SolarSystem */
		public SolarSystem click(int x, int y) {
			SolarSystem system = null;
			for (Circle circ : circles) {
				if (((x > circ.x) && (x < circ.x + circ.rad))
						&& ((y > circ.y) && (y < circ.y + circ.rad))) {
					system = circ.system;
				}

			}

			return system;
		}
	}
}
